﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzureTracer.ViewModels;
using System.ComponentModel;

namespace AzureTracer.Views.Controls
{
    public partial class SearchControl : UserControl
    {      
        public SearchControl()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            Loaded += SearchControl_Loaded;
            Searcher = new SearchViewModel();
        }

        void SearchControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.IsInDesignTool) return;
        }

        public SearchViewModel Searcher
        {
            get { return LayoutRoot.DataContext as SearchViewModel; }
            set { LayoutRoot.DataContext = value; }
        }
      

        #region SearchCommand DependencyProperty
        /// <summary>
        /// Gets or sets the value of the <see cref="SearchCommand" />
        /// property. This is a dependency property.
        /// </summary>
        public ICommand SearchCommand
        {
            get { return (ICommand)GetValue(SearchCommandProperty); }
            set { SetValue(SearchCommandProperty, value); }
        }

        /// <summary>
        /// Identifies the <see cref="SearchCommand" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register(
            "SearchCommand", typeof(ICommand), typeof(SearchControl),
            new PropertyMetadata(null, SearchCommandChanged));

        private static void SearchCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var ctl = (SearchControl)o;
            ctl.Searcher.SearchCommand = ctl.SearchCommand;
        }
        
        #endregion

        #region IsVisible DependencyProperty
        public bool IsVisible
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsVisible", typeof(bool), typeof(SearchControl), new PropertyMetadata(false, IsVisibleChanged));

        private static void IsVisibleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            SearchControl c = o as SearchControl;

            if (c != null)
            {
                bool isVisible = (bool)e.NewValue;
                if (isVisible)
                    c.ShowKeyboard();
                else
                    c.Reset();
            }
        }
        
        #endregion

        public void ShowKeyboard()
        {
            SearchTextBox.Focus();
        }

        public void Reset()
        {
            Searcher.ResetText();
        }
    }
}
